﻿using System;
using System.Windows;
using System.Windows.Controls;
using SilveR.Views.StatsViews;

namespace SilveR
{
    public partial class AnalysesPage : Page
    {
        public AnalysesPage()
        {
            InitializeComponent();

            //register this page so it can be accessed anywhere...
            App app = (App)Application.Current;
            app.AnalysesPage = this;
        }

        private Visibility SetTabVisible(bool visible)
        {
            if (visible)
                return System.Windows.Visibility.Visible;
            else
                return System.Windows.Visibility.Collapsed;
        }

        private void btnNewAnalysis_Click(object sender, RoutedEventArgs e)
        {
            //display child window asking user what analysis they want
            AnalysisSelectorWindow analSelector = new AnalysisSelectorWindow();
            analSelector.Closed += new EventHandler(analSelector_Closed); //this event is where we process the result
            analSelector.Show();
        }

        private void analSelector_Closed(object sender, EventArgs e)
        {
            AnalysisSelectorWindow analSelector = (AnalysisSelectorWindow)sender;

            //if cancel button pressed then return
            if (analSelector.DialogResult.Value == false) return;

            IAnalysis analysis = null;

            if (analSelector.SelectedAnalysis == "Summary Statistics")
            {
                analysis = new SummaryStats();
            }
            else if (analSelector.SelectedAnalysis == "Single Measure Parametric Analysis")
            {
                analysis = new SingleMeasureGLM();
            }
            else if (analSelector.SelectedAnalysis == "Repeated Measures Parametric Analysis")
            {
                analysis = new RepeatedMeasures();
            }
            else if (analSelector.SelectedAnalysis == "P-Value Adjustment")
            {
                analysis = new PValueAdjustment();
            }
            else if (analSelector.SelectedAnalysis == "Non-Parametric Analysis")
            {
                analysis = new NonParametrics();
            }
            else if (analSelector.SelectedAnalysis == "Graphical Analysis")
            {
                analysis = new GraphicalAnalysis();
            }
            else if (analSelector.SelectedAnalysis == "Power Analysis")
            {
                analysis = new PowerAnalysis();
            }
            else if (analSelector.SelectedAnalysis == "Unpaired t-test Analysis")
            {
                analysis = new UnpairedTTest();
            }
            else if (analSelector.SelectedAnalysis == "Paired t-test Analysis")
            {
                analysis = new PairedTTest();
            }

            TabItem newTabItem = new TabItem();
            newTabItem.Header = analysis.AnalysisName;
            newTabItem.Content = new AnalysisView(analysis);
            tabControlAnalyses.Items.Add(newTabItem);
            newTabItem.IsSelected = true;

            SetButtonAndTabStatus();
        }

        private void SetButtonAndTabStatus()
        {
            //set status of control tab and run button
            btnRun.IsEnabled = (tabControlAnalyses.Items.Count > 0);
            btnClose.IsEnabled = (tabControlAnalyses.Items.Count > 0);
            tabControlAnalyses.Visibility = SetTabVisible(tabControlAnalyses.Items.Count > 0);
        }

        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            //get the currently selected tab and run the analysis on it
            TabItem selectedTab = (TabItem)tabControlAnalyses.SelectedItem;
            AnalysisView analysisView = (AnalysisView)selectedTab.Content;
            analysisView.RunAnalysis();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            TabItem tabPageToClose = (TabItem)tabControlAnalyses.SelectedItem;
            tabControlAnalyses.Items.Remove(tabPageToClose);

            SetButtonAndTabStatus();
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //resize the tab control
            double newHeight = e.NewSize.Height - 30;

            if (newHeight > 0)
                tabControlAnalyses.Height = newHeight;
            else
                tabControlAnalyses.Height = 0;
        }


        private bool firstLoad = true;
        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            if (firstLoad)
            {
                textAnimation.Begin();
                firstLoad = false;
            }
        }
    }
}